var allNotice = [];//全部公告-数组
var hasNewPop;//有新消息

/*弹窗提醒定时器间隔时间 默认 5分钟*/
var refreshInterval = 5 * 60 * 1000;

/*日期 转换为 Unix时间戳*/
function dateToUnix(string) {
    // var string = '2017-11-30 16:13:19';
    var f = string.split(' ', 2);
    var d = (f[0] ? f[0] : '').split('-', 3);
    var t = (f[1] ? f[1] : '').split(':', 3);
    var unix = (new Date(
        parseInt(d[0], 10) || null,
        (parseInt(d[1], 10) || 1) - 1,
        parseInt(d[2], 10) || null,
        parseInt(t[0], 10) || null,
        parseInt(t[1], 10) || null,
        parseInt(t[2], 10) || null
    )).getTime();
    return unix;
}

/*显示提醒*/
function showRemind() {
    if ($('.remindModal').width() == 0) {
        $('#remindModal').css({'width': '285px'});
    }
    if (hasNewPop) {
        $('#remindModal').addClass('shake-horizontal');
    }
    setTimeout(function () {
        $('.remindModal').removeClass('shake-horizontal');
        hasNewPop = false;
    }, 6 * 1000);
}

/*关闭弹窗和查看全部*/
function hideRemind() {
    $('#remindModal').css('width', 0);
    setTimeout(function () {
        $('#remindModal .list').empty();

    }, 1000);
    if (allNotice.length > 0) {
        $.each(allNotice, function (index, item) {
            markRead(item);
        });
    }
    allNotice.length = 0;
}

/*获取部署公告-接口返回当天数据*/
function getOARemind() {
    $.ajax({
        url: OA_SERVER_NAME + "cxf/noc/getnotice/getpop",
        type: "GET",
        dataType: "JSON",
        data: {"empid": userInfo.empCode},
        success: function (data) {
            $('#remindModal .list').css('maxHeight','none');
            $('#remindModal .list').empty();
            var currentTime = (new Date()).getTime();
            $.each(data, function (index, item) {
                var popstartdate = dateToUnix(item.popstartdate + ' ' + item.popstarttime);
                var popenddate = dateToUnix(item.popenddate + ' ' + item.popendtime);
                var isPop = currentTime > popstartdate && currentTime < popenddate;
                if (currentTime > popenddate && $.inArray(item.id, allNotice) != -1) {//超过弹出时间的公告剔除
                    allNotice = $.grep(allNotice, function (value) {
                        return value != item.id;
                    });
                }
                if (isPop && item.docisread == 0) {
                    // if ($('#remindModal .remind').length < 4) {
                    var $li = '<li><a id="' + item.id + '" class="notice-remind remind" target="view_window" href="noticedetail.html?ggid=' + item.id + '" onclick="markRead('+item.id+')"><p class="name-font">' + item.docsubject + '</p></a></li>';
                    $('#remindModal .list').append($li);
                    // }
                    if($('#remindModal .list .remind').length>=4){
                        $('#remindModal .list').css('maxHeight',$('#remindModal .list').outerHeight());
                    }
                    if ($.inArray(item.id, allNotice) == -1) {
                        hasNewPop = true;
                        allNotice.push(item.id);
                    }
                }
            });
        },
        complete: function () {
            if ($('#remindModal .remind').length >= 4) {
                $('#remindModal #notice_more').show();
            } else {
                $('#remindModal #notice_more').hide();
            }
            if ($('#remindModal .remind').length == 0) {
                hideRemind();
            }
            if ($('#remindModal .list .remind').length > 0) {
                showRemind();
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log('getgg:部署公告接口调用失败');
        }
    });
}

/*标记为已读*/
function markRead(popID) {
    $.ajax({
        url: OA_SERVER_NAME + "cxf/noc/getnotice/getpop",
        type: "GET",
        dataType: "JSON",
        data: {
            "empid": userInfo.empCode,//工号
            "ggid": popID//公告ID
        },
        success: function () {
            $('#remindModal #'+popID).parents('li').remove();
            allNotice = $.grep(allNotice, function(value) {
                return value != popID;
            });
            if($('#remindModal .list .remind').length < 4 && !$('#remindModal #notice_more').is(":hidden")){
                $('#remindModal #notice_more').hide();
            }else if($('#remindModal .list .remind').length == 0 ){
                hideRemind();
            }
            console.log(popID + '标记已读成功');
        },
        error: function () {
            console.log('标记已读失败');
        }
    });
}

/*请求提醒*/
function getRemind() {
    getOARemind();

}

$(function () {
    getRemind();
    //定时请求
    setInterval(function () {
        getRemind();
    }, refreshInterval);
});
